﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmChangeRoomStatus : BaseForm
    {
        public RoomInfo RoomInfo = null;
        private OrderInfo orderInfo = null;

        public FrmChangeRoomStatus()
        {
            InitializeComponent();
        }

        private void FrmChangeRoomStatus_Load(object sender, EventArgs e)
        {
            if (RoomInfo != null)
            {
                this.lblRoomNo.Text = RoomInfo.RoomNo;
                this.lblStatus.Text = RoomInfo.Status.ToString();

                orderInfo = BLLFactory<Order>.Instance.GetCurrentOrder(RoomInfo.RoomNo);
                if (orderInfo != null)
                {
                    decimal amount = GetAllConsumption();
                   // this.lblConsumption.Text = amount.ToString("C2");//计算消费金额
                    //this.lblDeposit.Text = orderInfo.DepositMoney.ToString("C2");
                }
            }
        }

        private decimal GetAllConsumption()
        {
            decimal amount = 0M;
            List<ConsumerListInfo> list = BLLFactory<Room>.Instance.GetAllConsumption(RoomInfo.RoomNo);
            foreach (ConsumerListInfo info in list)
            {
                amount += info.Amount;
            }

            return amount;
        }

        #region 修改状态

        private OrderInfo CheckStatus()
        {
            OrderInfo attchOrderInfo = null;
            List<OrderInfo> orderList = BLLFactory<Order>.Instance.GetRelatedOrders(RoomInfo.RoomNo);
            if (orderList.Count <= 1)
            {
                MessageUtil.ShowTips("没有可寄托的主单，不能改变其状态");
                return attchOrderInfo;
            }

            foreach (OrderInfo info in orderList)
            {
                if (orderInfo.ID != info.ID)
                {
                    attchOrderInfo = info;//任意一个不同的订单，作为主单
                }
            }

            if (attchOrderInfo == null)
            {
                MessageUtil.ShowTips("没有可寄托的主单，不能改变其状态");
                return attchOrderInfo;
            }

            string message = string.Format("此宾客还没有结账。\r\n若要改变此房间的状态，其账单将被添加到房间{0}宾客的账单上！", attchOrderInfo.RoomNo);
            if (MessageUtil.ShowYesNoAndWarning(message) == DialogResult.Yes)
            {
                return attchOrderInfo;
            }
            else
            {
                return null;
            }           
        }

        private void tsbAvailable_Click(object sender, EventArgs e)
        {
            if (orderInfo == null)
            {
                BLLFactory<Room>.Instance.SetRoomStatus(RoomInfo.RoomNo, RoomStatus.空净房.ToString());
                this.DialogResult = DialogResult.OK;
                return;
            }

            this.DialogResult = DialogResult.None;
            OrderInfo attachOrderInfo = CheckStatus();
            if (attachOrderInfo == null)
            {
                return;
            }

            bool result = BLLFactory<Room>.Instance.AttachRoomOrders(orderInfo, attachOrderInfo,
                RoomStatus.空净房, true);
            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void tsbBookin_Click(object sender, EventArgs e)
        {
            if (orderInfo == null)
            {
                BLLFactory<Room>.Instance.SetRoomStatus(RoomInfo.RoomNo, RoomStatus.预定房.ToString());
                this.DialogResult = DialogResult.OK;
                return;
            }

            this.DialogResult = DialogResult.None;
            OrderInfo attachOrderInfo = CheckStatus();
            if (attachOrderInfo == null)
            {
                return;
            }
            bool result = BLLFactory<Room>.Instance.AttachRoomOrders(orderInfo, attachOrderInfo,
                RoomStatus.预定房, true);
            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void tsbStop_Click(object sender, EventArgs e)
        {
            if (orderInfo == null)
            {
                BLLFactory<Room>.Instance.SetRoomStatus(RoomInfo.RoomNo, RoomStatus.维修房.ToString());
                this.DialogResult = DialogResult.OK;
                return;
            }

            this.DialogResult = DialogResult.None;
            OrderInfo attachOrderInfo = CheckStatus();
            if (attachOrderInfo == null)
            {
                return;
            }
            bool result = BLLFactory<Room>.Instance.AttachRoomOrders(orderInfo, attachOrderInfo,
                RoomStatus.维修房, true);
            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void tsbClean_Click(object sender, EventArgs e)
        {
            if (orderInfo == null)
            {
                BLLFactory<Room>.Instance.SetRoomStatus(RoomInfo.RoomNo, RoomStatus.空脏房.ToString());
                this.DialogResult = DialogResult.OK;
                return;
            }

            this.DialogResult = DialogResult.None;
            OrderInfo attachOrderInfo = CheckStatus();
            if (attachOrderInfo == null)
            {
                return;
            }
            bool result = BLLFactory<Room>.Instance.AttachRoomOrders(orderInfo, attachOrderInfo,
                RoomStatus.空脏房, true);
            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void tsbInUse_Click(object sender, EventArgs e)
        {
            if (orderInfo == null)
            {
                BLLFactory<Room>.Instance.SetRoomStatus(RoomInfo.RoomNo, RoomStatus.住净房.ToString());
                this.DialogResult = DialogResult.OK;
                return;
            }

            this.DialogResult = DialogResult.None;
            OrderInfo attachOrderInfo = CheckStatus();
            if (attachOrderInfo == null)
            {
                return;
            } 
            bool result = BLLFactory<Room>.Instance.AttachRoomOrders(orderInfo, attachOrderInfo,
                RoomStatus.住净房, true);
            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void tsbContract_Click(object sender, EventArgs e)
        {
            //if (orderInfo == null)
            //{
            //    BLLFactory<Room>.Instance.SetRoomStatus(RoomInfo.RoomNo, RoomStatus.长包房.ToString());
            //    this.DialogResult = DialogResult.OK;
            //    return;
            //}

            //this.DialogResult = DialogResult.None;
            //OrderInfo attachOrderInfo = CheckStatus();
            //if (attachOrderInfo == null)
            //{
            //    return;
            //} 
            //bool result = BLLFactory<Room>.Instance.AttachRoomOrders(orderInfo, attachOrderInfo,
            //    RoomStatus.长包房, true);
            //if (result)
            //{
            //    this.DialogResult = DialogResult.OK;
            //}
        } 
        #endregion
    }
}
